package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.Message;
import com.hengxinguotong.zhihuichengjian.picturebrowser.view.TaskBigImgActivity;
import com.hengxinguotong.zhihuichengjian.treeview.*;
import com.hengxinguotong.zhihuichengjian.treeview.RecyclerAdapter;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/2/27.
 */

public class WorkTrendAdapter extends RecyclerAdapter<Message, WorkTrendAdapter.WorkTrendViewHolder> {

    public WorkTrendAdapter(Context context, List<Message> dataList) {
        super(context, dataList);
    }

    @Override
    public WorkTrendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_work_trend, null);
        return new WorkTrendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(WorkTrendViewHolder holder, int position) {
        holder.bindData(position);
    }

    public class WorkTrendViewHolder extends RecyclerAdapter.BaseViewHolder<Message>{
        @BindView(R.id.name_tv)
        HXTextView nameTv;
        @BindView(R.id.content_tv)
        HXTextView contentTv;
        @BindView(R.id.pic_rv)
        RecyclerView picRv;
        @BindView(R.id.no_pic_tv)
        HXTextView noPicTv;
        @BindView(R.id.date_tv)
        HXTextView dateTv;

        public WorkTrendViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void bindData(final int position) {
            final Message workTrend = dataList.get(position);
            nameTv.setText(workTrend.getConstructionName());
            contentTv.setText(workTrend.getContent());
            if(workTrend.getPicList() != null && workTrend.getPicList().size() > 0){
                noPicTv.setVisibility(View.GONE);
                picRv.setVisibility(View.VISIBLE);
                picRv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
                RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(workTrend.getPicList(), context);
                picRv.removeAllViews();
                picRv.setAdapter(recyclerViewAdapter);
                //设置间隔，我们自定义了一个DividerItemDecoration
                SpacesItemDecoration decoration=new SpacesItemDecoration (30);
//            picRv.addItemDecoration(decoration);
                //定义列表项增加，删除，重排序的动画，提供了默认的实现DefaultItemAnimator，效果为平移动画。
                picRv.setItemAnimator(new DefaultItemAnimator());
                //设置短点击监听
                recyclerViewAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
                    @Override
                    public void onClick(RecyclerView.ViewHolder VH, int position) {
                        TaskBigImgActivity.startActivity(context, (ArrayList<String>) workTrend.getPicList(), position);
                    }
                });
            }else{
                noPicTv.setVisibility(View.VISIBLE);
                picRv.setVisibility(View.GONE);
            }
//        startDate.setText(workTrend.getStartDate());
            dateTv.setText(workTrend.getCreateTime());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position);
                }
            });
        }
    }
}
